#include "state.h"
#include "process.h"

#include <iostream>
#include <string.h>

using namespace std;

int compare(char *result, char *resultOK, int length, char *name);

int main(int argc, char *argv[])
{
    Process process(argc, argv);

    char *result, *resultOK;

    if (argc > 2) {
        result = argv[1];
        resultOK = argv[2];
    } else  {
        cout << "Error. forgot parametr" << endl;
        exit(1);
    }

    compare(result, resultOK, 8,  "nstate_8");
    compare(result, resultOK, 12, "nstate_12");
    compare(result, resultOK, 16, "nstate_16");

    compare(result, resultOK, 8, "state_8_1");
    compare(result, resultOK, 8, "state_8_5");
    compare(result, resultOK, 8, "state_8_8");

    compare(result, resultOK, 12, "state_12_1");
    compare(result, resultOK, 12, "state_12_7");
    compare(result, resultOK, 12, "state_12_12");

    compare(result, resultOK, 16, "state_16_1");
    compare(result, resultOK, 16, "state_16_9");
    compare(result, resultOK, 16, "state_16_16");

    return 0;
}

int compare(char *result, char *resultOK, int length, char *name)
{
    int answer = 1;
    char *fullname = new char [strlen(result) + 20];
    State state_1(length), state_2(length), state_4(length), state_8(length), state_16(length), state_32(length), state_OK(length);

    sprintf(fullname, "%s/%s_1", result, name);
    state_1.input(fullname);
    sprintf(fullname, "%s/%s_2", result, name);
    state_2.input(fullname);
    sprintf(fullname, "%s/%s_4", result, name);
    state_4.input(fullname);
    sprintf(fullname, "%s/%s_8", result, name);
    state_8.input(fullname);
    sprintf(fullname, "%s/%s_16", result, name);
    state_16.input(fullname);
    sprintf(fullname, "%s/%s_32", result, name);
    state_32.input(fullname);
    sprintf(fullname, "%s/%s", resultOK, name);
    state_OK.input(fullname);

    if (state_1 != state_2) {
        if (state_1.getRank() == 0)
            cout << "Now we compare" << name << " state_1 != state_2" << endl;
        answer = 0;
    }
    if (state_1 != state_4) {
        if (state_1.getRank() == 0)
            cout << "Now we compare" << name << " state_1 != state_4" << endl;
        answer = 0;
    }
    if (state_1 != state_8) {
        if (state_1.getRank() == 0)
            cout << "Now we compare" << name << " state_1 != state_8" << endl;
        answer = 0;
    }
    if (state_1 != state_16) {
        if (state_1.getRank() == 0)
            cout << "Now we compare" << name << " state_1 != state_16" << endl;
        answer = 0;
    }
    if (state_1 != state_32) {
        if (state_1.getRank() == 0)
            cout << "Now we compare" << name << " state_1 != state_32" << endl;
        answer = 0;
    }
    if (state_2 != state_4) {
        if (state_2.getRank() == 0)
            cout << "Now we compare" << name << " state_2 != state_4" << endl;
        answer = 0;
    }
    if (state_2 != state_8) {
        if (state_2.getRank() == 0)
            cout << "Now we compare" << name << " state_2 != state_8" << endl;
        answer = 0;
    }
    if (state_2 != state_16) {
        if (state_2.getRank() == 0)
            cout << "Now we compare" << name << " state_2 != state_16" << endl;
        answer = 0;
    }
    if (state_2 != state_32) {
        if (state_2.getRank() == 0)
            cout << "Now we compare" << name << " state_2 != state_32" << endl;
        answer = 0;
    }
    if (state_4 != state_8) {
        if (state_4.getRank() == 0)
            cout << "Now we compare" << name << " state_4 != state_8" << endl;
        answer = 0;
    }
    if (state_4 != state_16) {
        if (state_4.getRank() == 0)
            cout << "Now we compare" << name << " state_4 != state_16" << endl;
        answer = 0;
    }
    if (state_4 != state_32) {
        if (state_4.getRank() == 0)
            cout << "Now we compare" << name << " state_4 != state_32" << endl;
        answer = 0;
    }
    if (state_8 != state_16) {
        if (state_8.getRank() == 0)
            cout << "Now we compare" << name << " state_8 != state_16" << endl;
        answer = 0;
    }
    if (state_8 != state_32) {
        if (state_8.getRank() == 0)
            cout << "Now we compare" << name << " state_8 != state_32" << endl;
        answer = 0;
    }
    if (state_16 != state_32) {
        if (state_16.getRank() == 0)
            cout << "Now we compare" << name << " state_16 != state_32" << endl;
        answer = 0;
    }

    if (answer != 0 && state_1 != state_OK) {
        if (state_1.getRank() == 0)
            cout << "We have problem with last result" << endl;
        answer = 0;
    }

    return answer;
}
